Page({
  data: {
      date: '2024-01-01',
      endDate: new Date().toISOString().split('T')[0],
      hours: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
      hourIndex: 0,
      eightCharacters: ''
  },
  bindDateChange: function (e) {
      this.setData({
          date: e.detail.value
      });
  },
  bindHourChange: function (e) {
      this.setData({
          hourIndex: e.detail.value
      });
  },
  getBirthEightCharacters() {
      const [year, month, day] = this.data.date.split('-').map(Number);
      const hour = this.data.hours[this.data.hourIndex];
      if (!year || !month || !day || hour === undefined) {
          wx.showToast({
              title: '请输入完整的出生日期和时辰',
              icon: 'none'
          });
          return;
      }
      const eightCharacters = this.calculateBirthEightCharacters(year, month, day, hour);
      this.setData({
          eightCharacters
      });
  },
  calculateBirthEightCharacters(year, month, day, hour) {
      const heavenlyStems = ['甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸'];
      const earthlyBranches = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥'];
      const solarTerms = [
          '小寒', '大寒', '立春', '雨水', '惊蛰', '春分', '清明', '谷雨', '立夏', '小满', '芒种', '夏至',
          '小暑', '大暑', '立秋', '处暑', '白露', '秋分', '寒露', '霜降', '立冬', '小雪', '大雪', '冬至'
      ];
      // 获取年柱
      function getYearColumn(year) {
          const stemIndex = (year - 4) % 10;
          const branchIndex = (year - 4) % 12;
          return heavenlyStems[stemIndex] + earthlyBranches[branchIndex];
      }
      // 获取月柱（简化，未考虑节气精确计算）
      function getMonthColumn(year, month) {
          const baseStem = [5, 7, 9, 1, 3];
          const stemIndex = (baseStem[(year % 5)] + month - 1) % 10;
          const branchIndex = (month + 1) % 12;
          return heavenlyStems[stemIndex] + earthlyBranches[branchIndex];
      }
      // 简化的日柱计算（实际需更复杂算法或查万年历）
      function getDayColumn(year, month, day) {
          // 这里简单返回示例，实际需实现精确算法
          return '示例日柱';
      }
      // 获取时柱
      function getHourColumn(hour) {
          const hourMap = {
              23: 0, 0: 0, 1: 1, 2: 1, 3: 2, 4: 2, 5: 3, 6: 3, 7: 4, 8: 4, 9: 5, 10: 5,
              11: 6, 12: 6, 13: 7, 14: 7, 15: 8, 16: 8, 17: 9, 18: 9, 19: 10, 20: 10, 21: 11, 22: 11
          };
          const branchIndex = hourMap[hour];
          // 时柱天干需结合日柱天干计算，这里简化
          const stemIndex = 0;
          return heavenlyStems[stemIndex] + earthlyBranches[branchIndex];
      }
      const yearColumn = getYearColumn(year);
      const monthColumn = getMonthColumn(year, month);
      const dayColumn = getDayColumn(year, month, day);
      const hourColumn = getHourColumn(hour);
      return `${yearColumn} ${monthColumn} ${dayColumn} ${hourColumn}`;
  }
});